package com.yangchoi.jetpackcompose.ui.baseui

import android.annotation.SuppressLint
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.animation.core.tween
import androidx.compose.animation.slideInHorizontally
import androidx.compose.animation.slideOutHorizontally
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.padding
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
import androidx.compose.material.rememberScaffoldState
import androidx.compose.runtime.*
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.navigation.compose.currentBackStackEntryAsState
import com.google.accompanist.insets.navigationBarsPadding
import com.google.accompanist.insets.statusBarsPadding
import com.google.accompanist.navigation.animation.AnimatedNavHost
import com.google.accompanist.navigation.animation.composable
import com.google.accompanist.navigation.animation.rememberAnimatedNavController
import com.google.accompanist.pager.ExperimentalPagerApi
import com.yangchoi.jetpackcompose.GlobalEvent
import com.yangchoi.jetpackcompose.GlobalViewModel
import com.yangchoi.jetpackcompose.LocalGlobalViewModel
import com.yangchoi.jetpackcompose.route.RouteKey
import com.yangchoi.jetpackcompose.ui.main.page.ClassicPage
import com.yangchoi.jetpackcompose.ui.main.page.HomePage
import com.yangchoi.jetpackcompose.ui.main.page.LoginPage
import com.yangchoi.jetpackcompose.ui.main.page.MinePage
import com.yangchoi.jetpackcompose.ui.main.page.ProjectPage
import com.yangchoi.jetpackcompose.ui.main.page.WebViewPage
import com.yangchoi.jetpackcompose.utils.Base64Util


@OptIn(ExperimentalComposeUiApi::class,
    ExperimentalFoundationApi::class,
    ExperimentalPagerApi::class,
    ExperimentalAnimationApi::class
)
@Composable
fun MainScaffoldConfig(globalViewModel: GlobalViewModel){
    val navHostController = rememberAnimatedNavController()
    val navBackStackEntry by navHostController.currentBackStackEntryAsState()
    val currentDestination = navBackStackEntry?.destination

    CompositionLocalProvider(LocalGlobalViewModel provides globalViewModel) {
        Scaffold(
            modifier = Modifier
                .statusBarsPadding()
                .navigationBarsPadding(),
            bottomBar = {
                when(currentDestination?.route){
                    RouteKey.HOME.toString() -> BottomTabBar(navHostController = navHostController, selectIndex = globalViewModel.globalState.bottomIndex){
                        globalViewModel.dispatch(event = GlobalEvent.OnUpdateBottomIndex, positionIndex = it)
                    }
                    RouteKey.PROJECT.toString() -> BottomTabBar(navHostController = navHostController, selectIndex = globalViewModel.globalState.bottomIndex){
                        globalViewModel.dispatch(event = GlobalEvent.OnUpdateBottomIndex, positionIndex = it)
                    }
                    RouteKey.NAVI.toString() -> BottomTabBar(navHostController = navHostController, selectIndex = globalViewModel.globalState.bottomIndex){
                        globalViewModel.dispatch(event = GlobalEvent.OnUpdateBottomIndex, positionIndex = it)
                    }
                    RouteKey.MINE.toString() -> BottomTabBar(navHostController = navHostController, selectIndex = globalViewModel.globalState.bottomIndex){
                        globalViewModel.dispatch(event = GlobalEvent.OnUpdateBottomIndex, positionIndex = it)
                    }
                }
            },
            content = { innerPadding ->
                AnimatedNavHost(  // 有动画的导航 composable 要使用对应的有动画的
                    navController = navHostController,
                    startDestination = RouteKey.HOME.toString(),
                    modifier = Modifier
                        .padding(innerPadding)
                        .background(MaterialTheme.colors.background),
                    enterTransition = {
                        slideInHorizontally(
                            initialOffsetX = {fullWidth -> fullWidth },
                            animationSpec = tween(500)
                        )
                    },
                    exitTransition = {
                        slideOutHorizontally(
                            targetOffsetX = {fullWidth -> -fullWidth },
                            animationSpec = tween(500)
                        )
                    },
                    popEnterTransition = {
                        slideInHorizontally(
                            initialOffsetX = {fullWidth -> -fullWidth },
                            animationSpec = tween(500)
                        )
                    },
                    popExitTransition = {
                        slideOutHorizontally(
                            targetOffsetX = {fullWidth -> fullWidth },
                            animationSpec = tween(500)
                        )
                    }
                ){
                    composable(route = RouteKey.HOME.toString()) {
                        HomePage(navHostController)
                    }

                    composable(route = RouteKey.PROJECT.toString()) {
                        ProjectPage(navHostController = navHostController)
                    }

                    composable(route = RouteKey.NAVI.toString()) {
                        ClassicPage(navHostController = navHostController)
                    }

                    composable(route = RouteKey.MINE.toString()) {
                        // 这里执行判断,如果 globalViewModel.globalState.bottomIndex == 0
                        // 也就是某些操作之后需要显示首页,比如退出登录
                        // 就需要跳转到首页,因为将bottomIndex == 0之后底部按钮的显示会是0
                        val shouldNavigateToHome = globalViewModel.globalState.bottomIndex
                        if (shouldNavigateToHome == 0) {
                            LaunchedEffect(Unit) {
                                navHostController.navigate(RouteKey.HOME.toString())
                                globalViewModel.globalState = globalViewModel.globalState.copy(bottomIndex = 0)
                            }
                        } else {
                            MinePage(navHostController = navHostController)
                        }
                        MinePage(navHostController = navHostController)
                    }

                    composable(route = RouteKey.WEB_VIEW.toString() + "/{url}") { entity ->
                        val args = entity.arguments
                        val url = args?.getString("url")
                        WebViewPage(navHostController = navHostController,url = Base64Util.base64ToString("$url"))
                    }

                    composable(route = RouteKey.LOGIN.toString()) {
                        LoginPage(navHostController = navHostController)
                    }
                }
//            NavHost( // 无动画的导航 composable 要使用对应的无动画的
//                modifier = Modifier.background(MaterialTheme.colors.background),
//                navController = navHostController,
//                startDestination = RouteKey.HOME.toString(),
//            ) {
//            }
            },
        )
    }

}